/**
 * @title RabbitApplication.java
 * @package com.esun.rabbit2
 * @description 
 * @author liugan
 * @date 2012-7-16
 * @copyright 深圳市易讯天空
 */
package com.esun;

import java.util.Properties;
import java.util.Random;

import org.androidpn.client.Constants;
import org.androidpn.client.NotificationService;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.esun.config.Config;
import com.esun.ui.R;

/**
 * @title RabbitApplication
 * @description application类，用于保存全局数据，提供全局功能
 * @author liugan
 */
public class RabbitApplication extends Application {

	private SharedPreferences sharedPrefs;
	// private boolean isMainActivityStated = false;
	/** 收接收到激活注册的推送通知 */
	private boolean receivedRegisteNotification = false;
	/** 收接收到开奖信息的推送通知 */
	private boolean receivedLotteryNotification = false;
	/** 收接收到活动推广的推送通知 */
	private boolean receivedActivityNotification = false;
	/** 推送功能是否可用 */
	private boolean isSetNotificationEnable = true;

	public boolean isReceivedRegisteNotification() {
		return receivedRegisteNotification;
	}

	public void setReceivedRegisteNotification(boolean receivedRegisteNotification) {
		this.receivedRegisteNotification = receivedRegisteNotification;
	}

	public boolean isReceivedLotteryNotification() {
		return receivedLotteryNotification;
	}

	public void setReceivedLotteryNotification(boolean receivedLotteryNotification) {
		this.receivedLotteryNotification = receivedLotteryNotification;
	}

	public boolean isReceivedActivityNotification() {
		return receivedActivityNotification;
	}

	public void setReceivedActivityNotification(boolean receivedActivityNotification) {
		this.receivedActivityNotification = receivedActivityNotification;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		initPushConfig();
	}

	// public boolean isMainActivityStated() {
	// return isMainActivityStated;
	// }

	// public void setMainActivityStated(boolean isMainActivityStated) {
	// this.isMainActivityStated = isMainActivityStated;
	// }

	public void startPushService() {
		Log.i("RabbitApplication", "startPushService");
		this.startService(NotificationService.getIntent());
	}

	public void stopPushService() {
		Log.i("RabbitApplication", "stopPushService");
		this.stopService(NotificationService.getIntent());
	}

	private void initPushConfig() {

		String callbackActivityPackageName = null;
		String callbackActivityClassName = null;

		sharedPrefs = getSharedPreferences(Config.CLIENT_PREFERENCES, Context.MODE_PRIVATE);
		Properties props = loadProperties();
		final String version = "0.5.0";
		final String apiKey = props.getProperty("apiKey", "");
		final String xmppHost = props.getProperty("xmppHost", "121.14.110.48");
		final String xmppPort = props.getProperty("xmppPort", "5222");

		TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		// Get deviceId
		String deviceId = telephonyManager.getDeviceId();
		// If running on an emulator
		if (deviceId == null || deviceId.trim().length() == 0 || deviceId.matches("0+")) {
			if (sharedPrefs.contains("EMULATOR_DEVICE_ID")) {
				deviceId = sharedPrefs.getString(Constants.EMULATOR_DEVICE_ID, "");
			} else {
				deviceId = (new StringBuilder("EMU")).append((new Random(System.currentTimeMillis())).nextLong()).toString();
			}
		}

		Editor editor = sharedPrefs.edit();
		editor.putString(Constants.API_KEY, apiKey);
		editor.putString(Constants.VERSION, version);
		editor.putString(Constants.XMPP_HOST, xmppHost);
		editor.putInt(Constants.XMPP_PORT, Integer.parseInt(xmppPort));
		editor.putString(Constants.DEVICE_ID, deviceId + "_61");
		// editor.putString(Constants.DEVICE_ID, "12345678");
		editor.putInt(Constants.NOTIFICATION_ICON, R.drawable.logo500wan);
		editor.putString(Constants.CALLBACK_ACTIVITY_PACKAGE_NAME, callbackActivityPackageName);
		editor.putString(Constants.CALLBACK_ACTIVITY_CLASS_NAME, callbackActivityClassName);
		editor.commit();
	}

	private Properties loadProperties() {

		Properties props = new Properties();
		try {
			int id = getResources().getIdentifier("androidpn", "raw", getPackageName());
			props.load(getResources().openRawResource(id));
		} catch (Exception e) {
			// Log.e(LOGTAG, "Could not find the properties file.", e);
			// e.printStackTrace();
		}
		return props;
	}

	@Override
	public void onTerminate() {
		this.stopPushService();
		super.onTerminate();
	}

	/**
	 * @title getPushSharedPrefs
	 * @description
	 * @return
	 */
	public SharedPreferences getPushSharedPrefs() {
		return sharedPrefs;
	}

	/**
	 * @title closeSetNotification
	 * @description
	 */
	public void closeSetNotification() {
		receivedRegisteNotification = false;
		receivedLotteryNotification = false;
		receivedActivityNotification = false;
		isSetNotificationEnable = false;

	}

	public void openSetNotification() {
		isSetNotificationEnable = true;
	}

	public boolean isSetNotificationEnable() {
		return isSetNotificationEnable;
	}
}
